//
//  StayBehindContainerViewController.m
//  Utils
//
//  Created by enghou on 2018/4/27.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "StayBehindContainerViewController.h"
#import "StayBehindBusinessViewController.h"
@interface StayBehindContainerViewController ()

@end

@implementation StayBehindContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)deleAction{
    if ([self.ids length] > 0) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/leftBehindPeople/deleteLeftBehindPeople.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                [(BaseViewController *)self.theDelete performSelector:@selector(beginRefresh)];
                [self.navigationController popToViewController:(UIViewController *)self.theDelete animated:YES];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }
}
-(void)addNewWithFilePath:(NSString *)path{
    NSMutableDictionary *param = [self param];
    if (param) {
        if (path) {
            [param setObject:path forKey:@"population.fileStorage.path"];
        }
        [self beginLoading];
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/leftBehindPeople/addLeftBehindPeople.action" parameter:[self param] completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                [self.theDelete performSelector:@selector(beginRefresh)];
                [self pop];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }
}

-(void)modifyWithFilePath:(NSString *)path{
    NSMutableDictionary *param = [self param];
    if (param) {
        if (path) {
            [param setObject:path forKey:@"population.fileStorage.path"];
        }
        [self beginLoading];
        [[NetWorkerManager sharedInstance]getWithPath:@"/mobile/comprehensiveGovernance/leftBehindPeople/updateLeftBehindPeople.action" parameter:[self param] completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                [self.theDelete performSelector:@selector(beginRefresh)];
                [self pop];
            }else{
                [self alertSome:model.msg];
            }

        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }
}

-(void)loadData{
    if ([self.ids length] > 0) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/leftBehindPeople/getLeftBehindPeopleById.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                NSDictionary *data = [model.data objectForKey:@"data"];
                PopulationBaseInfoViewController *p = [self.ctlArray objectAtIndex:0];
                NSLog(@"%@",p.view);
                if (data[@"imgUrl"]) {
                    p.head_imageurl = data[@"imgUrl"];
                    [p.headImageView sd_setImageWithURL:[NSURL URLWithString:p.head_imageurl] placeholderImage:[UIImage imageNamed:@"icon_empty_head"]];
                }
                p.idCardField.text = data[@"idCardNo"];
                if ([data[@"idCardNo"] length] == 18) {
                    p.idCardField.enabled = NO;
                    p.calendarBtn.enabled = NO;
                }
                p.nameField.text = data[@"name"];
                p.sexField.text = data[@"gender"][@"displayName"];
                p.sexModel = [[PropertyDictItemModel alloc]init];
                p.sexModel.ids = [model contentForKey:@"id" withSource:data[@"gender"]];
                p.sexModel.displayName = [model contentForKey:@"displayName" withSource:data[@"gender"]];//data[@"gender"][@"displayName"];
                p.birthdayField.text = data[@"birthday"];
                p.locateTypeField.text = data[@"residenceType"][@"displayName"];
                p.registerTypeModel = [[PropertyDictItemModel alloc]init];
                p.registerTypeModel.displayName = data[@"residenceType"][@"displayName"];
                p.registerTypeModel.ids = [model contentForKey:@"id" withSource:data[@"residenceType"]];
                p.minzuField.text = data[@"nation"][@"displayName"];
                p.minzuModel = [[PropertyDictItemModel alloc]init];
                p.minzuModel.ids = [model contentForKey:@"id" withSource:data[@"nation"]];
                p.minzuModel.displayName = data[@"nation"][@"displayName"];
                p.hujiProvince = [[Province alloc]init];
                p.hujiProvince.code = [model contentForKey:@"province" withSource:data];//data[@"province"];
                p.hujiCity = [[City alloc]init];
                p.hujiCity.code = [model contentForKey:@"city" withSource:data];;
                p.hujiCountry = [[Country alloc]init];
                p.hujiCountry.code = [model contentForKey:@"district" withSource:data];;
                p.oldProvince = [[Province alloc]init];
                p.oldProvince.code = [model contentForKey:@"placeProvince" withSource:data];;
                p.oldCity = [[City alloc]init];
                p.oldCity.code = [model contentForKey:@"placeCity" withSource:data];
                p.oldCountry = [[Country alloc]init];
                p.oldCountry.code = [model contentForKey:@"placeDistrict" withSource:data];
                
                p.nowProvince = [[Province alloc]init];
                p.nowProvince.code = [model contentForKey:@"presentProvince" withSource:data];
                p.nowCity = [[City alloc]init];
                p.nowCity.code = [model contentForKey:@"presentProvince" withSource:data];
                p.nowCountry = [[Country alloc]init];
                p.nowCountry.code = [model contentForKey:@"presentProvince" withSource:data];
                
                p.politicalModel = [[PropertyDictItemModel alloc]init];
                p.politicalModel.ids = [model contentForKey:@"id" withSource:data[@"politicalBackground"]];
                
                p.marryModel = [[PropertyDictItemModel alloc]init];
                p.marryModel.ids = [model contentForKey:@"id" withSource:data[@"maritalState"]];
                
                p.eduDegreeModel = [[SubTypeItemModel alloc]init];
                p.eduDegreeModel.subTid = [model contentForKey:@"schoolVal" withSource:data];//data[@"schoolVal"];
                p.educationDegreeField.text = data[@"schoolStr"];
                
                p.occupyModel = [[PropertyDictItemModel alloc]init];
                p.occupyModel.ids = [model contentForKey:@"id" withSource:data[@"career"]];//data[@"career"][@"id"];
                
                p.religionModel = [[PropertyDictItemModel alloc]init];
                p.religionModel.ids = [model contentForKey:@"id" withSource:data[@"faith"]];
                
                p.zhengzhiField.text = data[@"politicalBackground"][@"displayName"];
                p.marriedField.text = data[@"maritalState"][@"displayName"];
                p.locateAddressField.text = data[@"domicilePlace"];
                p.homeLocateField.text = data[@"place"];
                p.homeLocateDetailField.text = data[@"nativePlaceAddress"];
                BOOL hasHouse = [data[@"isHaveHouse"] boolValue];
                p.nowLocateField.text = data[@"present"];
                if (hasHouse) {
                    p.yesBtn.enabled = YES;
                    p.yesBtn.selected = YES;
                    p.nowLocateDetailField.text = data[@"currentAddress"];
                    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*现住门(楼)详址" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];
                    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
                    p.nowLocateDetailField.placeholder = @"请输入门楼详址";
                    p.changeState.attributedText = target;
                }else{
                    p.noBtn.enabled = YES;
                    p.noBtn.selected = YES;
                    p.nowLocateDetailField.text = data[@"noHouseReason"];
                    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*无住房原因" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]}];
                    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:17]} range:NSMakeRange(0, 1)];
                    p.changeState.attributedText = target;
                    p.nowLocateDetailField.placeholder = @"请输无住房原因";
                }
                p.marriedField.text = data[@"maritalState"][@"displayName"];
                p.pastNameField.text = data[@"usedName"];
                p.phoneNumberField.text = [model contentForKey:@"mobileNumber" withSource:data];//data[@"mobileNumber"];
                p.stablePhoneField.text = [model contentForKey:@"telephone" withSource:data];//data[@"telephone"];
                p.occupyCategoryField.text = data[@"career"][@"displayName"];
                p.occupyField.text = data[@"careerText"];
                p.zongjiaoField.text = data[@"faith"][@"displayName"];
                p.serviceLocateField.text = data[@"workUnit"];
                StayBehindBusinessViewController *business = self.ctlArray[1];
                NSLog(@"%@",business.view);
                business.healthModel.ids = [model contentForKey:@"id" withSource:data[@"health"]];
                business.healthLabel.text = data[@"health"][@"displayName"];
                business.personalIncomeField.text = [model contentForKey:@"personalAnnualIncome" withSource:data];
                business.stayPeopleType.ids = [model contentForKey:@"id" withSource:data[@"leftType"]];
                business.stayPeopleTypeField.text = [model contentForKey:@"displayName" withSource:data[@"leftType"]];
                business.ruhuField.text = data[@"householdIdentity"][@"displayName"];
                business.ruhuModel.ids = [model contentForKey:@"id" withSource:data[@"householdIdentity"]];
                business.idCardField.text = data[@"familyMainIdCard"];
                business.nameField.text = data[@"familyMainName"];
                business.mainHealthField.text = data[@"familyMainHealth"][@"displayName"];
                business.mainHealthModel.ids = [model contentForKey:@"id" withSource:data[@"familyMainHealth"]];
                business.relationField.text = data[@"leftRelationship"][@"displayName"];
                business.relationModel.ids = [model contentForKey:@"id" withSource:data[@"leftRelationship"]];
                business.phoneNumberField.text = data[@"familyMainPhone"];
                business.stablePhoneField.text = data[@"familyTelephone"];
                business.incomeField.text = [model contentForKey:@"familyAnnualIncome" withSource:data];
                business.workAddressField.text = data[@"familyMainAdress"];
                business.difficultField.text = data[@"difficultiesAndDemands"];
                business.helpSituationField.text = data[@"helpingSituation"];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }
}

-(void)configViewControllers{
    PopulationBaseInfoViewController *p = self.ctlArray[0];
    p.paramPrefix = @"leftBehindPeople";
    [self.slide configTitles:@[@"基本信息",@"业务信息"]];
    StayBehindBusinessViewController *stay = (StayBehindBusinessViewController *)[VCLoader vcWithName:@"StayBehindBusinessViewController"];
    stay.theDelete = self;
    [self.ctlArray removeObject:self.ctlArray.lastObject];
    self.ctlArray[1] = stay;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
